#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include "simdisk.h"

int main(int argc, char **argv) {

   unsigned int nInodes, nBlocks;
   int retVal;

   if ((argc != 4) ||
       (sscanf(argv[2], "%u", &nInodes) != 1) ||
       (sscanf(argv[3], "%u", &nBlocks) != 1)
      ) {
      fprintf(stderr,
         "Usage: %s name_of_simdisk_file num_of_inodes num_of_datablocks\n",
         argv[0]);
      return -1;
   }

   if (strlen(argv[1]) > 254) {
      fprintf(stderr, "Error: Filename too long.\n");
      return -1;
   }

   if (nInodes % 64 != 0) {
      nInodes = ((nInodes + 64) / 64) * 64;
   }

   if(nInodes < 1 || nInodes > 1024) {
      fprintf(stderr,
         "Error: Keep the number of inodes between 64 and 1024.\n");
      return -1;
   }

   if(nBlocks < 1 || nBlocks > 1024) {
      fprintf(stderr,
         "Error: Keep the number of blocks between 64 and 1024.\n");
      return -1;
   }

   retVal = create_simdisk(argv[1], nInodes, nBlocks);

   if (retVal != 0) {
      fprintf(stderr, "Error: Couldn't create simdisk.\n");
      return -1;
   }
   
   printf("Simdisk %s created with %d inodes and %d blocks.\n",
      argv[1], nInodes, nBlocks);

   return 0;
}
